<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Code extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wl_code';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 短信验证
     * @param 手机 $tel
     * @param 验证码 $code
     * @param 用户操作类型 type 注册：reg  登录：login  找回密码：find  bind [绑定手机]  update[更改手机] update_pay[修改支付密码]
     */
    public static function check_code($tel, $code, $type = 'reg')
    {
        switch ($type) {
            case 'login':
                $type = 1;
                break;

            case 'find':
                $type = 2;
                break;

            case 'bind':
                $type = 3;
                break;

            case 'update':
                $type = 4;
                break;

            case 'update_pay':
                $type = 5;
                break;

            default:
                $type = 0;
                break;
        }

        $info = self::where('mobile', $tel)->where('code', $code)->where('type', $type)->first();

        if($info){

            $return = (time() - strtotime($info->updated_at))/1000 < 600 ? true : false;

        }else{

            $return = false;
        }

        return $return;
    }
    
}
